package com.ruoyi.biz.strategy.impl;

import com.ruoyi.biz.domain.BizGoodsCategory;
import com.ruoyi.biz.domain.dto.CaculateGoodsDto;
import com.ruoyi.biz.domain.vo.CaculateGoodsVo;
import com.ruoyi.biz.enums.CaculateTypeEnum;
import com.ruoyi.biz.strategy.IGoodsCategoryCaculateStrategy;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author tds
 * @version 1.0.0
 * @Company
 * @ClassName DefaultGoodsCategoryStrategy
 * @Description 商品类别默认计算策略
 * @Date 2022-11-10 22:59
 */
@Component("goodsCategoryDefaultStrategy")
public class GoodsCategoryDefaultStrategy implements IGoodsCategoryCaculateStrategy {

    /**
     * 计算商品类别价格信息
     *
     * @param caculateGoodsDto 商品类别价格信息
     * @param goodsCategory 商品类别信息
     * @return
     */
    @Override
    public CaculateGoodsVo caculate(CaculateGoodsDto caculateGoodsDto, BizGoodsCategory goodsCategory) {
        // 总价 = 单价 * 面积
        BigDecimal totalMoney = goodsCategory.getPrice().multiply(caculateGoodsDto.getSize())
                .setScale(2, BigDecimal.ROUND_HALF_UP);

        // 折扣价 = 总价 * 折扣 / 10
        BigDecimal discountMoney = totalMoney.multiply(caculateGoodsDto.getDiscount())
                .divide(new BigDecimal(10)).setScale(2, BigDecimal.ROUND_HALF_UP);

        // 最终价格 = 总价 - 折扣价
        BigDecimal finalMoney = totalMoney.subtract(discountMoney).setScale(2, BigDecimal.ROUND_HALF_UP);

        // 单价 * 面积 * 折扣
        return CaculateGoodsVo.builder()
                .totalMoney(totalMoney)
                .discountMoney(discountMoney)
                .finalMoney(finalMoney)
                .build();
    }

    /**
     * 获取计算类型
     *
     * @return
     */
    @Override
    public String getType() {
        return CaculateTypeEnum.DEFAULT.getCode();
    }
}
